﻿// IWebWF
// Copyright (c) 2008
// by Michael Washington
// Webmaster@ADefWebserver.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Microsoft.VisualBasic;

namespace IWebWFCore
{
    public class GeneralSettings
    {
        string _Version;
        bool _AnonymousLogin;
        int _MaxLoginAttempts;
        string _SMTPServer;
        string _SMTPAuthendication;
        bool _SMTPSecure;
        string _SMTPUserName;
        string _SMTPPassword;
        string _SMTPFromEmail;

        #region Public Properties
        public string Version
        {
            get { return _Version; }
        }

        public bool AnonymousLogin
        {
            get { return _AnonymousLogin; }
        }

        public int MaxLoginAttempts
        {
            get { return _MaxLoginAttempts; }
        }

        public string SMTPServer
        {
            get { return _SMTPServer; }
        }

        public string SMTPAuthendication
        {
            get { return _SMTPAuthendication; }
        }

        public bool SMTPSecure
        {
            get { return _SMTPSecure; }
        }

        public string SMTPUserName
        {
            get { return _SMTPUserName; }
        }

        public string SMTPPassword
        {
            get { return _SMTPPassword; }
        }

        public string SMTPFromEmail
        {
            get { return _SMTPFromEmail; }
        }
        #endregion

        public GeneralSettings()
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         select Settings;

            _AnonymousLogin = Convert.ToBoolean(resuts.FirstOrDefault(x => x.Setting == "AnonymousLogin").SettingValue);
            _MaxLoginAttempts = Convert.ToInt32(resuts.FirstOrDefault(x => x.Setting == "MaxLoginAttempts").SettingValue);
            _SMTPServer = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPServer").SettingValue);
            _SMTPAuthendication = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPAuthendication").SettingValue);
            _SMTPSecure = Convert.ToBoolean(resuts.FirstOrDefault(x => x.Setting == "SMTPSecure").SettingValue);
            _SMTPUserName = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPUserName").SettingValue);
            _SMTPPassword = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPPassword").SettingValue);
            _SMTPFromEmail = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPFromEmail").SettingValue);

            var versions = from Version in IWebWFDataContext.Versions
                           select Version;

            _Version = Convert.ToString(versions.FirstOrDefault().VersionNumber.ToString());
        }

        #region UpdateAnonymousLogin
        public static void UpdateAnonymousLogin(bool AnonymousLogin)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "AnonymousLogin"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(AnonymousLogin);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateMaxLoginAttempts
        public static void UpdateMaxLoginAttempts(int MaxLoginAttempts)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "MaxLoginAttempts"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(MaxLoginAttempts);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateSMTPServer
        public static void UpdateSMTPServer(string SMTPServer)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "SMTPServer"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPServer);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateSMTPAuthendication
        public static void UpdateSMTPAuthendication(string SMTPAuthendication)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "SMTPAuthendication"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPAuthendication);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateSMTPSecure
        public static void UpdateSMTPSecure(bool SMTPSecure)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "SMTPSecure"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPSecure);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateSMTPUserName
        public static void UpdateSMTPUserName(string SMTPUserName)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "SMTPUserName"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPUserName);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateSMTPPassword
        public static void UpdateSMTPPassword(string SMTPPassword)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "SMTPPassword"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPPassword);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion

        #region UpdateSMTPFromEmail
        public static void UpdateSMTPFromEmail(string SMTPFromEmail)
        {
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();
            var resuts = from Settings in IWebWFDataContext.IWebWFSettings
                         where Settings.Setting == "SMTPFromEmail"
                         select Settings;

            resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPFromEmail);
            IWebWFDataContext.SubmitChanges();
        }
        #endregion
    }
}